import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 导入路由对象, 安装顺序生成动态路由表
import departmentsRouter from './modules/departmentsRouter'
import settingRouter from './modules/settingRouter'
import employeesRouter from './modules/employeesRouter'
import permissionRouter from './modules/permissionRouter'

// 创建动态路由表
export const asyncRoutes = [
  // 放动态路由对象
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter]

// const asyncRoutes = [
//   {
//     path: '/salarys',
//     component: Layout,
//     children: [{
//       path: '',
//       name: 'salarys',
//       // 路由懒加载
//       component: () => import('@/views/salarys/salarys.vue'),
//       meta: { title: '工资', icon: 'money' }
//     }]
//   },
//   {
//     path: '/social_securitys',
//     component: Layout,
//     children: [{
//       path: '',
//       name: 'social_securitys',
//       // 路由懒加载
//       component: () => import('@/views/social_securitys/social_securitys.vue'),
//       meta: { title: '社保', icon: 'table' }
//     }]
//   },
//   {
//     path: '/attendances',
//     component: Layout,
//     children: [{
//       path: '',
//       name: 'attendances',
//       // 路由懒加载
//       component: () => import('@/views/attendances/attendances.vue'),
//       meta: { title: '考勤', icon: 'skill' }
//     }]
//   },
//   {
//     path: '/approvals',
//     component: Layout,
//     children: [{
//       path: '',
//       name: 'approvals',
//       // 路由懒加载
//       component: () => import('@/views/approvals/approvals.vue'),
//       meta: { title: '审批', icon: 'tree-table' }
//     }]
//   }

// ]

// 静态路由表
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 最终生成的路由
  // 将动态路由表和静态路由表合并在一起
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
